﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.IO;

namespace WeiBeeCommon.Core
{
    public class VisitedHelper
    {
        public VisitedHelper(string filename)
        {
            VisitedFilename = filename;
        }
        private List<string> _visitedUserIdList;
        public List<string> VisitedUserIdList
        {
            get
            {
                if (_visitedUserIdList == null)
                {
                    _visitedUserIdList = LoadVisitedUserIdListFromFile();
                }
                return _visitedUserIdList;
            }
        }
        private string _visitedFilename;
        private string VisitedFilename
        {
            get
            {
                if (string.IsNullOrEmpty(_visitedFilename))
                {
                    _visitedFilename = "visited.xml";
                }
                return _visitedFilename;
            }
            set
            {
                _visitedFilename = value;
            }

        }
        private List<string> LoadVisitedUserIdListFromFile()
        {
            var result = new List<string>();
            if (!File.Exists(VisitedFilename)) return result;
            XmlSerializer ser = new XmlSerializer(typeof(List<string>));
            using (FileStream fs = new FileStream(VisitedFilename, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    result = ser.Deserialize(fs) as List<string>;
                    result = result.Select(s => s).Distinct().ToList();
                }
                catch
                {
                }
            }
            return result;
        }

        private const int _visitedListLimit = 180000;
        private int cacheCount = 0;
        private const int cacheSize = 1;
        public void AppendToVisited(string id)
        {
            AppendToVisited(new List<string> { id });
        }
        public void AppendToVisited(List<string> idList)
        {
            if (VisitedUserIdList.Count >= _visitedListLimit)
            {
                int remove = VisitedUserIdList.Count + idList.Count - _visitedListLimit;
                VisitedUserIdList.RemoveRange(0, remove);
            }
            VisitedUserIdList.AddRange(idList);
            cacheCount += idList.Count;
            if (cacheCount >= cacheSize)
            {
                Save();
                cacheCount = 0;
            }
        }
        private void Save()
        {
            if (VisitedUserIdList.Count >= _visitedListLimit)
            {
                int remove = VisitedUserIdList.Count - _visitedListLimit;
                VisitedUserIdList.RemoveRange(0, remove);
            }
            XmlSerializer ser1 = new XmlSerializer(typeof(List<string>));
            using (FileStream fs1 = new FileStream(VisitedFilename, FileMode.Create, FileAccess.Write))
            {
                ser1.Serialize(fs1, VisitedUserIdList);
            }
        }
    }
}
